package com.example.demo.service.impl;

import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.IResponseMapper;
import com.example.demo.param.vo.ResponseVO;
import com.example.demo.service.IResponseService;
import com.example.demo.util.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ResponseServiceImpl implements IResponseService {

    @Autowired private IResponseMapper responseMapper;

    @Override
    public ResponseVO getMyRes(String userId, Integer page, Integer pageSize) throws ServiceException {
        if(page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请提供正确的分页!");
        }
        ResponseVO vo = this.responseMapper.getMyRes(userId,page,pageSize);
        vo.setPage(page);
        vo.setPageSize(pageSize);
        return vo;
    }

    @Override
    public Boolean insert(String userId, String questionId, String responseContent) throws ServiceException {
        if (Judge.isNull(questionId) || Judge.isNull(responseContent)){
            throw new ServiceException("请务必完善必填项!");
        }
        Integer affect = this.responseMapper.insert(Judge.getUUID(),userId,questionId,responseContent,0,new Date());
        if (affect != 1){
            throw new ServiceException("回答失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException {
        if (Judge.isNull(id)){
            throw new ServiceException("请提供正确的id!");
        }
        Integer affect = this.responseMapper.delete(id);
        if (affect != 1){
            throw new ServiceException("删除失败!");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean update(String userId, String responseContent) throws ServiceException {
        if (Judge.isNull(userId) || Judge.isNull(responseContent)){
            throw new ServiceException("请务必完善必填项!");
        }
        Integer affect = this.responseMapper.update(Judge.getUUID(),responseContent);
        if (affect != 1){
            throw new ServiceException("修改问题失败!");
        }
        return Boolean.TRUE;
    }
}
